import sys
import common 

def simple_type(t):
	if t.endswith('byte') or t.endswith('short') or t.endswith('int') or  t.endswith('long'):
		t=1
	else:
		t=0
	return t
	
def print_getfix(f):	
	ret_type=common.get_ret_type(f[1])
	field_name=common.get_name(f[0])
	if f[2]!='' and not ret_type.endswith('*'):
		is_array=1
		ret_type=ret_type+'*'
	else:
		is_array=0

	print('\t//%s'%(f[3]))
	print('\t%s Get%s()'%(ret_type,field_name))
	if is_array==0 and  ret_type.endswith('*'):	
		print('\t{return &%s;}'%(f[0]))
	else:
		print('\t{return %s;}'%(f[0]))

def print_getvar(f):	
	ret_type=common.get_ret_type(f[1])
	field_name=common.get_name(f[0])
	if f[2]!='' and not ret_type.endswith('*'):
		ret_type=ret_type+'*'

	print('\t//%s'%(f[3]))
	print('\t%s Get%s();'%(ret_type,field_name))
				
def print_field(f):	
	c=common.get_field_type(f[1])
	print('\t//%s'%(f[3]))
	if f[2]!='':
		if f[2]=='*':
			s=''
		else:
			s=f[2]
		print('\t%s %s[%s];'%(c,f[0],s))
	else:
		print('\t%s %s;'%(c,f[0]))	
	
#if len(sys.argv)<2:
#     print("usage:")
#     exit(0) 
#inf=sys.argv[1]

inf='dex-def.txt'


myfile = open(inf, "r")
fixed=[]
var=[]
l=[]
ll=[]
f=0
count=''
for line in (myfile):
	s=line.strip(' \t\n')
	if len(s)<1:
		continue
		
	if s.startswith('#'):
		if len(s)>1 and s.endswith('#'):
			#print(s)
			n=s[1:-1]
			n1=n.split(' ',1)
			#print(n)
			#print(n1)
			if len(n1)>1:
				name=common.get_cls_name(n1[0])
			else:
				name=common.get_cls_name(n)	
			fixed=[]
			var=[]
			
			l=[name,fixed,var]
			ll.append(l)
			f=0			
		continue 
	
	s1=s.split('\t',1)
	field=s1[0]
	s2=s1[1].split('\t',1)
	t=s2[0]

	if len(s2)>1:
		comment=s2[1]
	else:
		comment=''
		
	t1=t.split(' ',1)
	if len(t1)>1:
		t=t1[0].strip(' ')
		comment=comment+'('+t1[1]+')'	
				
	i=t.find('[')
	if i!=-1:
		count=t[i+1:-1]
		t=t[:i]
		if count=='':
			count='*'
	else:
		count=''
	if f==0:
		if simple_type(t)!=1: #t=='String' or t.endswith('128') or t=='encoded_value': 
			f=1
		elif count!='' and not count[0].isdigit():
			f=1
	

	if f==0:
		fixed.append([field,t,count,comment]) 
	else:
		var.append([field,t,count,comment])
			
print('''
#ifndef dex_structs_h
#define dex_structs_h
#include "dex-types.h"
	''')		
for x in ll:
	print('class %s;'%(x[0]))

print('class CDexClass;')
print('class CDexAnnotation;')
print('class CDexMethod;')
print('class CDexString;')
print('class CDexEncodedValue;')

for x in ll:
	print('''class %s
{
'''%(x[0]))
	if len(x[1])==0:	
 		print('\tchar dummy;')	
	print('''
public:''')	
	for y in x[1]:
		print_field(y)

	print('/*')
	for y in x[2]:
		print_field(y)	
	print('*/')


	for y in x[1]:
		print_getfix(y)
	for y in x[2]:
		print_getvar(y)						
		
	print('''
	int Size();
	void print();
	int sprint(char *buffer);	
};''')	
	
print('''
typedef CDexClassdefItem CDexClassDefs;
#endif// dex_structs_h
	''')